package com.test.calc;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qindi.mvc.Reply;
import com.qindi.mvc.beans.response.ResponseDialog;
import com.qindi.mvc.beans.response.ResponseToaster;
import com.qindi.mvc.beans.response.ResponseViewPanel;
import com.qindi.mvc.handler.EventHandler;
import com.qindi.mvc.handler.Map;

public class Basic extends EventHandler {
	
	@Map(include=true)
	public Reply getCalc(ServletContext cs, HttpServletRequest request, HttpServletResponse response)throws Exception
	{				
		Reply reply = new Reply();		
		ResponseViewPanel v = new ResponseViewPanel();
		v.payload.put("url", "views/calc.jsp");
		int a = 1/0;
		reply.addResponse(v);
		reply.supressViewForward();
		return reply;
	}
	
	@Map(include=true)
	public Reply add(ServletContext cs, HttpServletRequest request, HttpServletResponse response)throws Exception
	{
		Reply reply = new Reply();
		float a = 0;
		float b = 0;		
		a = getParamFloat(request,"a");
		b = getParamFloat(request,"b");			
		reply.add(new ResponseToaster(1, a + " + " + b + " = " + (a+b)));
		//reply.supressViewForward();
		return reply;
	}
	
	@Map(include=true)
	public Reply sub(ServletContext cs, HttpServletRequest request, HttpServletResponse response)throws Exception
	{	
		Reply reply = new Reply();
		int a = getParamInt(request,"a");
		int b = getParamInt(request,"b");				
		reply.add(new ResponseToaster(1, a + " - " + b + " = " + (a-b) ));
		reply.supressViewForward();		
		return reply;
	}
	
	@Map(include=true)
	public Reply mul(ServletContext cs, HttpServletRequest request, HttpServletResponse response)throws Exception
	{	
		Reply reply = new Reply();
		int a = getParamInt(request,"a");
		int b = getParamInt(request,"b");			
		reply.add(new ResponseToaster(1, a + " x " + b + " = " + (a*b) ));
		reply.supressViewForward();
		return reply;
	}
	
	@Map(include=true)
	public Reply div(ServletContext cs, HttpServletRequest request, HttpServletResponse response)throws Exception
	{
		Reply reply = new Reply();
		int a = getParamInt(request,"a");
		int b = getParamInt(request,"b");				
		reply.add(new ResponseDialog("Result",a + " / " + b + " = " + (a/b),null));
		reply.supressViewForward();
		
		return reply;
	}	
		
	public Reply doNothing(ServletContext cs, HttpServletRequest request, HttpServletResponse response)throws Exception
	{		
		Reply reply = new Reply();
		return reply;
	}
}
